PreferredItems = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0", "AceDB-2.0", "AceDebug-2.0")

local L = AceLibrary("AceLocale-2.2"):new("PreferredItems")
local TipHooker = AceLibrary("TipHooker-1.0")
local ItemBonusLib = AceLibrary("ItemBonusLib-1.0")
local Waterfall = AceLibrary("Waterfall-1.0")

local statGroup = {
	[1] = {
		name = "base",
	},
	[2] = {
		name = "physical",
	},
	[3] = {
		name = "spell",
	},
	[4] = {
		name = "defense",
	},
}

local options = {
	type = "group",
	args = {
		config = {
			order 	= 1,
			type	= "execute",
			name	= L["Configuration"],
			desc	= L["Open the PreferredItems configuration GUI."],
			func	= function() Waterfall:Open("PreferredItems") end,
			wfHidden = true,
		},
		mod = {
			order	= 2,
			type	= "group",
			name	= L["Stat Modifiers"],
			desc	= L["Adjust stat weighting for PI calculation."],
			args	= {},
		},
			
	},
}

local defaults = {
	userMods = {
		['*'] = 0
	}
}

function PreferredItems:OnInitialize()
    -- Called when the addon is loaded
	self:RegisterDB("PreferredItemsDB", "PreferredItemsDBPC")
	self:RegisterDefaults('char', defaults)
	self:RegisterChatCommand("/preferreditems", "/pi", options)
	Waterfall:Register("PreferredItems", "aceOptions", options, "colorR", 0.8, "colorG", 0.8, "colorB", 0.9, "treeType", "SECTIONS")
end

function PreferredItems:OnEnable()
	self:SetDebugging(true)

	TipHooker:Hook(AddToTooltip, "item")
	PopulateOptions()
end

function PreferredItems:OnDisable()
    -- Called when the addon is disabled
end

function AddToTooltip(tooltip, ...)
	local item = select(2, tooltip:GetItem())
	if (IsEquippableItem(item)) then
		tooltip:AddLine("PI: " .. CalculatePI(item))
	end
	tooltip:Show()
end

function CalculatePI(item)
	local piValue = 0
	item = Disenchant(item)
	local itemStats = ItemBonusLib:ScanItem(item, true)
	for stat, mod in pairs(PreferredItems.db.char.userMods) do
		if (itemStats[stat]) then
			piValue = piValue + (itemStats[stat] * PreferredItems_Data.stat[stat].mod * mod)
		end
	end
	return floor((piValue * 100) + .5) / 100
end

function Disenchant(item)
	return string.gsub(item, "(item:%d+:)%d+(:%d+:%d+:%d+:%d+:%-?%d+:%-?%d+)", "%10%2")
end

function GetUserModPercent(stat)
	return PreferredItems.db.char.userMods[stat]*100
end

function SetUserModPercent(stat, percent)
	if (percent == 0) then
		PreferredItems.db.char.userMods[stat] = nil
	else
		PreferredItems.db.char.userMods[stat] = percent/100
	end
end

function PopulateOptions()
	for group, args in pairs(statGroup) do
		options.args.mod.args[args.name] = {
			order	= group,
			type	= "group",
			name	= L["GroupName:"..args.name],
			desc	= L["GroupDesc:"..args.name],
			args 	= {}
		}
	end
	for stat, args in pairs(PreferredItems_Data.stat) do
		options.args.mod.args[statGroup[args.group].name].args[stat] = {
			order 	= args.order,
			type	= "range",
			name	= L["StatName:"..stat],
			desc	= L["StatDesc:"..stat],
			min	= 0,
			max	= 100,
			step	= 1,
			get	= function() return GetUserModPercent(stat) end,
			set	= function(v) SetUserModPercent(stat, v) end,
		}
	end
end